<template>
<div>
  <!--  分隔-->
  <el-row>
    <el-col :span="24"><div class="grid-content bg-purple-light">
      <div style="float: left; margin: 20px 100px; font-weight: 700;color:#606266;font-size: 20px"><i class="el-icon-search"></i>异常查询</div>
    </div></el-col>
  </el-row>
    <!--  查询条件-->
    <el-form :inline="true" :model="queryForm" style="text-align: left; margin: 0 10%">
      <el-form-item label="调度单号">
        <el-input v-model="queryForm.ticketId" placeholder="调度单号" clearable></el-input>
      </el-form-item>
      <el-form-item label="异常类型">
        <el-select v-model="queryForm.abnormalType" placeholder="请选择">
          <el-option
            v-for="item in statusList"
            :key="item"
            :label="item"
            :value="item">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="info" @click="getList" plain>查询</el-button>
        <el-button type="info" @click="queryFormReset()" plain>重置</el-button>
      </el-form-item>
    </el-form>
  <!--  分隔-->
  <el-row>
    <el-col :span="24"><div class="grid-content bg-purple-light">
      <div style="float: left; margin: 0 100px; font-weight: 700;color:#606266;font-size: 20px"><i class="el-icon-s-unfold"></i>数据列表</div>
    </div></el-col>
  </el-row>
  <div id="s" class="animate__animated animate__zoomIn animate__delay-0.5s" style="margin-top: 20px">
  <div id = "list">
    <el-table :data="tableData" stripe style="width: 100%" :border="true" :header-cell-style="{background:'#209e91', color:'#ffffff',textAlign:'center'}">
      <el-table-column
        prop="id"
        label="序号"
        align="center"
        width="50px">
      </el-table-column>
      <el-table-column
        prop="aid"
        label="异常单号"
        align="center">
      </el-table-column>
      <el-table-column
        prop="ticketId"
        label="调度单号"
        align="center">
      </el-table-column>
      <el-table-column
        prop="abnormalType"
        label="异常类型"
        align="center">
      </el-table-column>
      <el-table-column
        prop="abnormalTime"
        label="异常时间"
        align="center">
      </el-table-column>
      <el-table-column
        prop="abnormalAddress"
        label="异常地点"
        align="center">
      </el-table-column>
      <el-table-column
        prop="dispatchAcceptResult.acceptResult"
        label="受理结果"
        align="center">
      </el-table-column>
      <el-table-column
        prop="status"
        label="状态"
        align="center"
      width="100px">
      </el-table-column>
      <el-table-column
        label="操作"
        width="100">
        <template v-slot="scope">
          <el-button @click="accept(scope.row)" type="text" size="small"  >受理</el-button>
          <el-button @click="check(scope.row)" type="text" size="small"  >查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-row>
      <el-col :span="24"><div class="grid-content bg-purple-light">
        <div style="margin: 20px"></div>
      </div></el-col>
    </el-row>
    <!--  分页-->
    <el-pagination
      @size-change="getList"
      @current-change="getList"
      :current-page.sync="queryForm.pageNum"
      :page-sizes="[10, 20, 30, 40, 60, 80, 100]"
      :page-size.sync="queryForm.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    <el-row>
      <el-col :span="24"><div class="grid-content bg-purple-light">
        <div style="margin: 20px"></div>
      </div></el-col>
    </el-row>
  </div>
    <!--  受理/查看页面-->
    <el-dialog
      :title="title"
      :visible.sync="show"
      width="30%">
      <span>
        <el-form :model="form" :rules="rules" ref="ruleForm" label-width="80px">
          <el-form-item label="调度单号">
            <el-input v-model="form.ticketId" placeholder="" disabled clearable></el-input>
          </el-form-item>
          <el-form-item label="异常时间">
            <el-input v-model="form.abnormalTime" placeholder="" disabled clearable></el-input>
          </el-form-item>
          <el-form-item label="异常类型">
            <el-input v-model="form.abnormalType" placeholder="" disabled clearable></el-input>
          </el-form-item>
          <el-form-item label="异常地点">
            <el-input v-model="form.abnormalAddress" placeholder="" disabled clearable></el-input>
          </el-form-item>
          <el-form-item label="异常描述">
            <el-input v-model="form.abnormalDescribe" placeholder="" disabled clearable></el-input>
          </el-form-item>
          <el-form-item label="受理结果">
             <el-select v-model="form.acceptResult"  @change="distributeCar($event)"  placeholder="请选择" style="width: 100%" :disabled="disabled">
                <el-option
                  v-for="item in acceptResultList"
                  :key="item.id"
                  :label="item.acceptResult"
                  :value="item.id">
                </el-option>
             </el-select>
          </el-form-item>
          <el-form-item label="委派司机" v-if="this.showDistribute">
             <el-select v-model="form.number"    placeholder="请选择" style="width: 100%" >
                <el-option
                  v-for="item in this.carFormList"
                  :key="item.number"
                  :label="item.anotherOne"
                  :value="item.number">
                </el-option>
             </el-select>
          </el-form-item>
          <el-form-item label="受理描述">
            <el-input type="textarea" v-model="form.acceptDescribe" placeholder="受理描述" :disabled="disabled" clearable></el-input>
          </el-form-item>
        </el-form>
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button  @click="show = false">取 消</el-button>
        <el-button type="primary" @click="submit()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</div>
</template>

<script>
export default {
  name: "DispatchMaintenance",
  data() {
    return {
      carForm:{},
      carFormList:[],
      showDistribute:false,
      //查询对象
      queryForm: {
        dispatchTicket: {
          carNumber: ""
        },
        ticketId:'',
        abnormalType:"",
        pageNum: 1,
        pageSize: 10
      },
      //表格数组
      tableData: [],
      //共多少条
      total: 0,
      // 下拉状态列表
      statusList:[],

      //校验规则
      rules: {
        acceptResult: [{required: true, message: '请输入受理结果', trigger: 'blur'}],
        acceptDescribe: [{required: true, message: '请输入维保类型', trigger: 'blur'}],
      },

      //新增与编辑表格
      form:{

        dispatchTicket:{
          carInfo:{}
        },
        dispatchAcceptResult:{
          id:'',
          acceptResult:''
        },
        acceptResult:''
      },
      //显示新增、修改
      show: false,
      //标题
      title: '',
      disabled: false,

      //受理结果
      acceptResultList:[],
      //显示是否重新派车
      showCar:false,
      showCarList:false,
      carList:[],
      queryCarList:{
        carStatus:'',
        number:'',
        pageNum: 1,
        pageSize: 10
      },
      carListTotal:0,
      selectCar:{}
    }
  },
  methods: {
    distributeCar(val){
      if (val==2){
        this.showDistribute=true
        this.$axios({
          url: 'car/list',
          method: 'post',
          data: {carStatus:"空闲",pageNum: 1, pageSize: 100},
        }).then(res => {
          if (res.data.success) {
            this.carFormList=res.data.object.list

          }
        }).catch(err => {
          this.$message.error(err.message);
        })
      }else {
        this.showDistribute=false
      }


    },
    getList() {
      this.$axios({
        url: 'dispatch/abnormal/list',
        method: 'post',
        data: this.queryForm
      }).then(res => {
        if (res.data.success) {
          this.tableData = res.data.object.list;
          this.total = res.data.object.total;

        }
      }).catch(err => {
        this.$message.error(err.message);
      })
    },
    getStatusList() {
      this.$axios({
        url: 'dispatch/abnormal/statusList',
        method: 'post',
      }).then(res => {
        if (res.data.success) {
          this.statusList = res.data.object;
        }
      }).catch(err => {
        this.$message.error(err.message);
      })
    },
    getAcceptResultList() {
      this.$axios({
        url: 'dispatch/abnormal/acceptResultList',
        method: 'post',
      }).then(res => {
        if (res.data.success) {
          this.acceptResultList = res.data.object;
        }
      }).catch(err => {
        this.$message.error(err.message);
      })
    },
    queryFormReset(){
      this.queryForm.abnormalType = null;
      this.queryForm.ticketId = null;
      this.queryForm.dispatchTicket.carNumber = null;
      this.getList();
    },
    success() {
      this.getList()
    },
    //查看
    check(row) {
      //设置标题
      this.title = '查看'
      //显示对话框
      this.show = true
      //清空表单
      this.form = {}
      //给form对象赋值
      this.form = Object.assign({}, row);

      //替换受理结果显示的值
      // this.form.acceptResult = this.form.dispatchAcceptResult.id;
      //清空校验结果
      this.$nextTick(() => {
        this.$refs['ruleForm'].clearValidate();
      })
      this.disabled = true
    },
    //受理
    accept(row) {
      this.showDistribute=false
      this.title = '受理'
      //清空表单
      this.form = {}

      console.log(row)
      //给form对象赋值
      this.form = Object.assign({}, row);
      // this.form.acceptResult = "";
      this.disabled = false;

      //给受理结果赋值
      if(this.form.dispatchAcceptResult != undefined){
        this.form.acceptResult = this.form.dispatchAcceptResult.id
      }else{
        // this.form.acceptResult = this.acceptResultList.id
      }
      if(this.form.acceptResult == "2"){
        this.showCar = true;
      }
      //显示对话框
      this.show = true
      //清空校验结果
      // this.$nextTick(() => {
      //   this.$refs['ruleForm'].clearValidate();
      // })
    },
    submit() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          //校验通过
          if (this.title == '受理') {
            this.$refs['ruleForm'].validate((valid) => {
              if (valid) {
                this.$axios({
                  url: 'dispatch/abnormal/update',
                  method: 'post',
                  data: this.form
                }).then(res => {
                  if (res.data.success) {
                    this.show = false;
                    this.getList();
                    //调用父路由方法，刷新异常数
                    this.$emit('father')
                  } else {
                    this.$message.error(res.data.message);
                  }
                }).catch(err => {
                  this.$message.error(err.message);
                })
              }
            });
          } else {
            this.show = false;
            // this.$axios({
            //   url: 'dispatch/abnormal/check',
            //   method: 'post',
            //   data: this.form
            // }).then(res => {
            //   if (res.data.success) {
            //     this.show = false;
            //     this.getList()
            //   } else {
            //     this.$message.error(res.data.message);
            //   }
            // }).catch(err => {
            //   this.$message.error(err.message);
            // })
          }
        }
      });
    },
    //取到选定的汽车
    handleCurrentChange(val) {
      this.selectCar = val;
    },
  },
  created() {
    this.getList();
    this.getStatusList();
    //this.getAcceptResultList();
  }
}
</script>

<style scoped>
.el-button--info:hover {
  background: #209e91;
  border-color: #209e91;
  color: #FFF;
}
#s{
  width:90%;
  margin:10px auto;
  background-color: #ffffff;
  box-shadow: 10px 10px 5px #c4c6c7;
  border-radius: 5px
}
</style>
